package
{
	/**
	 * ...
	 * @author wj
	 */
	import org.flixel.FlxG
	import flash.events.Event;
	import flash.geom.Point;
	import org.flixel.FlxSprite;
	
	//import Tools;
	
	public class MyTank1 extends MyTank
	{
		//public static var num:int = 0;
		private var time:Number = 0;
		private var turn:int = 61;
		public static var run:Boolean = true;
		private var chktime:Number = 0;
		public function MyTank1(vthe:Tank)
		{
			super(vthe);
			
			id = 1;
			speed = 2 * 30;
			score = 100;
			
			the.addEventListener(move);
		
		}
		
		public function shot():void
		{
			var bx:int;
			var by:int;
			switch (the.frame + 1)
			{
				case 1: 
					bx = the.x + (the.width / 2) - 3;
					by = the.y;
					break;
				case 2: 
					bx = the.x + (the.width / 2) - 3;
					by = the.y + the.width;
					break;
				case 3: 
					bx = the.x;
					by = the.y + (the.width / 2) - 3;
					break;
				case 4: 
					bx = the.x + the.width;
					by = the.y + (the.width / 2) - 3;
					break;
			}
			
			var bullet:MyBullet = new MyBullet(this, new Bullet(bx, by), the.frame + 1, id, bulletSpeed);
			
			MyMain.instance.bullets.add(bullet.the);
		}
		
		public function changedir()
		{
			var olddir = dir;
			while (dir == olddir)
			{
				dir = Math.floor(Math.random() * 4) + 1
			}
			the.gotoAndStop(dir);
		}
		function juli(o1:FlxSprite,o2:FlxSprite):Number
		{
			return Math.sqrt((o1.x-o2.x)*(o1.x-o2.x)+(o1.y-o2.y)*(o1.y-o2.y))
		}
		function too2(o1:FlxSprite, o2:FlxSprite):int
		{
			var dir:int;
			if (Math.abs(o1.x - o2.x) < Math.abs(o1.y - o2.y))
			{
				if (o1.y < o2.y)
				{
					//down
					dir=1
				}
				else
				{
					dir =2
					
				}
			}
			else
			{
				if (o1.x < o2.x)
				{
					//right
					dir=4
				}
				else
				{
					dir = 3
				}
				
			}
			return dir
		}
		private function move():void
		{
			if (!run)
			{
				the.velocity.x = 0
				the.velocity.y = 0;
				return
			}
			
			time += FlxG.elapsed;
			chktime += FlxG.elapsed;
			if (chktime > 3)//cooldown
			{
				if (MyMain.myTank == null)
				{
				}
				else
				{
					//if (juli(the, MyMain.myTank.the) < 200)
					//{
						//this.dir = too2(the, MyMain.myTank.the);
						//the.gotoAndStop(dir);
						//this.chktime = 0;
					//}
					this.dir = too2(the, MyMain.myTank.the);
						the.gotoAndStop(dir);
						this.chktime = 0;
				}
			}
			if (the.alive)
			{
				if (time > 1)
				{
					shot();
					time = 0;
				}
			}
			
			//var i:int = the.frame+1;
			//while (i == the.frame+1)
			//i = Math.random() * 4 + 1;
			switch (dir)
			{
				case 1: 
				{
					//if (canMoveUp())
					//the.y -= speed;
					the.acceleration.x = 0
					the.velocity.x = 0
					the.velocity.y = -speed;
					//else
					//the.gotoAndStop(i);
					break;
				}
				case 2: 
				{
					//if (canMoveDown())
					the.velocity.x = 0
					the.acceleration.x = 0
					the.velocity.y = speed; //the.y += speed;
					//else
					//the.gotoAndStop(i);
					break;
				}
				case 3: 
				{
					//if (canMoveLeft())
					the.velocity.x = -speed //the.x -= speed;
					the.acceleration.y = 0
					the.velocity.y = 0
					//else
					//the.gotoAndStop(i);
					break;
				}
				case 4: 
				{
					//if (canMoveRight())
					the.acceleration.y = 0
					the.velocity.y = 0
					the.velocity.x = speed
					//the.x += speed;
					//else
					//the.gotoAndStop(i);
					break;
				}
				default: 
					break;
			}
			if (the.x < MyMain.ori.x)
				the.x = MyMain.ori.x + the.width / 2
			if (the.y < MyMain.ori.y)
				the.y = MyMain.ori.y + the.width / 2
			if (the.x > MyMain.ori.x + 26 * 16)
				the.x = MyMain.ori.x + 26 * 16 - the.width / 2
			if (the.y > MyMain.ori.y + 26 * 16)
				the.y = MyMain.ori.y + 26 * 16 - the.width / 2
		}
		
		public override function destroy():void
		{
			the.kill();
			bomb();
			if (isBomb)
			{
				if (!FlxG.mute)
					MySound.boom1.play();
			}
			
			Map.setNum(Map.getNum() - 1);
			MyMain.instance.showtxt();
		}
	}

}